from app.extensions import db
from app.modules.common.utils import JsonEncoder


class User(db.Model, JsonEncoder):
    __tablename__ = "user"
    id = db.Column(db.String, primary_key = True)
    account = db.Column(db.String, nullable = False, unique = True)
    realname = db.Column(db.String, nullable = False)
    role = db.Column(db.String, nullable = False)
    password = db.Column(db.String, nullable = False)
    zt_user_id = db.Column(db.String, nullable = True)
    zt_user_account = db.Column(db.String, nullable = True)
    email = db.Column(db.String, nullable = True)
    role_list = None
    login_ip = None

    def __init__(self, id, account, realname, role, password, zt_user_id, zt_user_account, email):
        self.id = id
        self.account = account
        self.realname = realname
        self.role = role
        self.password = password
        self.zt_user_id = zt_user_id
        self.zt_user_account = zt_user_account
        self.email = email

    def __repr__(self):
        return (
            "<{class_name}("
            "id={self.id}, "
            "account=\"{self.account}\", "
            "realname=\"{self.realname}\", "
            "role=\"{self.role}\", "
            "zt_user_id=\"{self.zt_user_id}\", "
            "zt_user_account=\"{self.zt_user_account}\", "
            "email=\"{self.email}\", "
            ")>".format(
                class_name = self.__class__.__name__,
                self = self
            )
        )

    def to_dict(self):
        return {
            'id': self.id,
            'account': self.account,
            'realname': self.realname,
            'role': self.role,
            'zt_user_id': self.zt_user_id,
            'zt_user_account': self.zt_user_account,
            'email': self.email,
        }
